package com.ruoyi.web.controller.system;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.service.ISysStudentLocusService;
import com.ruoyi.system.service.ISysStudentService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台信息信息操作处理
 *
 * @author zxy
 */
@Controller
@RequestMapping("/system/student")
public class SysStudentController extends BaseController {
    private String prefix = "system/student";

    @Autowired
    private ISysStudentService studentService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysStudentLocusService sysStudentLocusService;

    @RequiresPermissions("system:student:view")
    @GetMapping()
    public String config() {
        return prefix + "/student";
    }

    @RequiresPermissions("system:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysStudent user) {
        startPage();
        List<SysStudent> list = studentService.selectStudentList(user);
        return getDataTable(list);
    }

    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:user:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStudent user) {
        List<SysStudent> list = studentService.selectStudentList(user);
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        return util.exportExcel(list, "用户数据");
    }


    @RequiresPermissions("system:user:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        return util.importTemplateExcel("用户数据");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {

        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysStudent user) {
        user.setCreateBy(String.valueOf(getUserId()));
        return toAjax(studentService.insertStudent(user));
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("system:student:edit")
    @GetMapping("/edit/{studentId}")
    public String edit(@PathVariable("studentId") Long studentId, ModelMap mmap) {
        mmap.put("student", studentService.selectStudentById(studentId));
        return prefix + "/edit";
    }

    /**
     * 查询用户详细
     */
    @RequiresPermissions("system:student:list")
    @GetMapping("/view/{studentId}")
    public String view(@PathVariable("studentId") Long studentId, ModelMap mmap) {
        mmap.put("student", studentService.selectStudentById(studentId));
        return prefix + "/view";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysStudent user) {

        return toAjax(studentService.updateStudent(user));
    }


    @RequiresPermissions("system:student:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        if (ArrayUtils.contains(Convert.toLongArray(ids), getUserId())) {
            return error("当前用户不能删除");
        }
        return toAjax(studentService.deleteStudentByIds(ids));
    }


    @RequiresPermissions("system:student:development")
    @GetMapping("/development/{studentId}")
    public String development(@PathVariable("studentId") Long studentId, ModelMap mmap) {
        mmap.put("student", studentService.selectStudentById(studentId));
        return prefix + "/development";
    }

    @RequiresPermissions("system:student:development")
    @Log(title = "成长记录", businessType = BusinessType.UPDATE)
    @PostMapping("/development")
    @ResponseBody
    public AjaxResult developmentSave(SysStudentLocus student) {
        sysStudentLocusService.developmentSave(student);
        return success();

    }

    /**
     * 选择用户
     */
    @GetMapping("/selectUser")
    public String selectUser(SysUser user, ModelMap mmap) {
        userService.selectUserList(user);
        return prefix + "/selectUser";
    }
    @RequiresPermissions("system:student:development")
    @GetMapping("/development/detail/{studentId}")
    public String developmentDetail(@PathVariable("studentId") Long studentId, ModelMap mmap) {
        mmap.put("studentId", studentId);
        return prefix + "/development/data";
    }

}
